<?php
class Mymodel extends Model {
	
    function Mymodel()
    {
        parent::Model();
    }
    
    /* To test your Queries*/ 
    function show_the_query()
    {
		echo '<pre>';
	 	print_r($this->db->last_query());
		echo '</pre>';
	}
    		
	function get_data($table_name, $id=NULL, $condition=NULL , $order_by=NULL,$order_type='DESC')
	{
		if($id==NULL && is_array($condition))
		{
			foreach ($condition as $key => $val)
			{
				$this->db->where($key, $val);
			}

                        //if the order_by var passed in the function then
                        //it should resurn the query result as the order was given!
                        if(isset($order_by)){
                            $order = $order_by;
                        $this->db->order_by($order, $order_type);
                        }

			$query = $this->db->get($table_name);
			$result = $query->result_array();
			$result['count'] = $query->num_rows();
			
                        //$this->show_the_query();
                        
			return (( is_array($result) && count($result) > 0) ? $result : array() );
		}
		
		//Case 1: get info by ID
		if(isset($id) && !empty($id))
			{
				$query = $this->db->get_where($table_name,  array('id' => $id));
				$result = $query->result_array();
				return (( is_array($result) && count($result) > 0) ? $result[0] : array() );
			}
		// End Case 1
		
		//Case 2: get all info
		if($id ==NULL && !is_array($condition))
		{
			$query = $this->db->get_where($table_name);
			//check if there is no data
			$result['count'] = $query->num_rows();
			$result['info'] = $query->result_array(); ;
			return $result;
		}
		// End Case 2
	}
	
function get_data_joined($table_name,$table_name2,$join_condition, $id=NULL, $condition=NULL , $order_by=NULL)
	{
		
		if($id==NULL && is_array($condition))
		{
			
			
			
			
			foreach ($condition as $key => $val)
			{
				$this->db->where($key, $val);
			}
			
			
			

                        //if the order_by var passed in the function then
                        //it should resurn the query result as the order was given!
                        if(isset($order_by)){
                            $order = $order_by;
                        $this->db->order_by($order, 'DESC');
                        }
            $this->db->from($table_name);
            $this->db->join($table_name2, $join_condition);
			$query = $this->db->get();
			$result = $query->result_array();
			$result['count'] = $query->num_rows();
			
                        //$this->show_the_query();
                        
			return (( is_array($result) && count($result) > 0) ? $result : array() );
		}
		
		//Case 1: get info by ID
		if(isset($id) && !empty($id))
			{
			#	$this->db->from($table_name);
                $this->db->join($table_name2, $join_condition);
				$query = $this->db->get_where($table_name,  array('id' => $id));
				$result = $query->result_array();
				return (( is_array($result) && count($result) > 0) ? $result[0] : array() );
			}
		// End Case 1
		
		//Case 2: get all info
		if($id ==NULL && !is_array($condition))
		{
			
			#$this->db->from($table_name);
            $this->db->join($table_name2, $join_condition);
            
			$query = $this->db->get($table_name);;
			
			#$this->show_the_query();
			//check if there is no data
			$result['count'] = $query->num_rows();
			$result['info'] = $query->result_array(); ;
			return $result;
		}
		// End Case 2
	}
	function insert_new_record($table_name, $data)
	{
		//print_r($data);
		$result = $this->db->insert($table_name, $data);
                 //$this->show_the_query();
                 return $result;
	}
	
		
	function update_by_id($table_name, $data, $id=NULL ,$condition=NULL)
	{
		if($id==NULL && is_array($condition)){
			foreach ($condition as $key => $val)
			{
				$this->db->where($key, $val);
			}
			$result = $this->db->update($table_name, $data);
			//$this->show_the_query();
			return $result;
		}
		else
		return $this->db->update($table_name, $data, array('id' => $id));
	}
	
	function delete_by_id($table_name, $id )
	{	
		return $this->db->delete($table_name, array('id' => $id));
	}
	
	function check_by_col($table_name, $col_name, $col_value)
	{
		$this->db->where($col_name, $col_value);
		$this->db->from($table_name);
		return $this->db->count_all_results();
	}
	
	
}

